package sg.edu.nus.iss.phoenix.scheduledprogram.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import sg.edu.nus.iss.phoenix.authenticate.dao.UserDao;
import sg.edu.nus.iss.phoenix.core.exceptions.NotFoundException;
import sg.edu.nus.iss.phoenix.dao.DAOFactory;
import sg.edu.nus.iss.phoenix.radioprogram.dao.RadioProgramDAO;
import sg.edu.nus.iss.phoenix.schedule.dao.AnnualScheduleDAO;
import sg.edu.nus.iss.phoenix.schedule.dao.ProgramSlotDAO;
import sg.edu.nus.iss.phoenix.schedule.dao.WeeklyScheduleDAO;
import sg.edu.nus.iss.phoenix.schedule.entity.AnnualSchedule;
import sg.edu.nus.iss.phoenix.schedule.entity.ProgramSlot;
import sg.edu.nus.iss.phoenix.schedule.entity.WeeklySchedule;

/**
 * 
 * @author Wang Shunde
 * 
 */
public class ReviewSelectScheduledProgramService {
	private DAOFactory daoFactory;
	private AnnualScheduleDAO annualScheduleDAO;
	private WeeklyScheduleDAO weeklyScheduleDAO;
	private ProgramSlotDAO programSlotDAO;
	private RadioProgramDAO radioProgramDAO;
	private UserDao userDao;

	public ReviewSelectScheduledProgramService() {
		daoFactory = new DAOFactory();
		annualScheduleDAO = daoFactory.getAnnualScheduleDAO();
		weeklyScheduleDAO = daoFactory.getWeeklyScheduleDAO();
		programSlotDAO = daoFactory.getProgramSlotDAO();
		radioProgramDAO = daoFactory.getRadioProgramDAO();
		userDao = daoFactory.getUserDAO();
	}

	public AnnualSchedule searchAnnualSchedule(int schedule_year) {
		try{
		return annualScheduleDAO.searchMatching(schedule_year);
		}
		catch(SQLException e){return new AnnualSchedule();}
	}

	public List<WeeklySchedule> searchWeeklySchedule(
			int selectedAnnualScheduleId) {
		try{
		return weeklyScheduleDAO.searchMatching(selectedAnnualScheduleId);
		}
		catch(SQLException e){return new ArrayList<WeeklySchedule>();}
	}

	public List<ProgramSlot> searchProgramSlot(long selectedWeeklyScheduleId) {
		try{
			List<ProgramSlot> pslots = programSlotDAO.searchMatching(selectedWeeklyScheduleId);
			for(int i=0; i< pslots.size(); i++){
				pslots.get(i).setRadioProgram(radioProgramDAO.findById(pslots.get(i).getRadioProgramId()));
				pslots.get(i).setPresenter(userDao.getObject(pslots.get(i).getPresenterId()));
				pslots.get(i).setProducer(userDao.getObject(pslots.get(i).getProducerId()));
			}
			return pslots;
		}
		catch (NotFoundException e) {
			return new ArrayList<ProgramSlot>();
		}
		catch(SQLException e){return new ArrayList<ProgramSlot>();}
	}

	public List<AnnualSchedule> searchAllAnnualSchedule() {
		try{
			return annualScheduleDAO.searchMatching();
		}
		catch (SQLException e) {
			return new ArrayList<AnnualSchedule>();
		}
	}
}
